﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCM.UI.ChildWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCM.UI.Utils;

namespace Baosight.ZNH.ZMCM.UI
{
    public partial class ZMCM02 : EFViewCmes
    {
        private MaterialBasicInfoBlock _block = new MaterialBasicInfoBlock();//需要物资基础信息的所有Block 如 在用/作废
        public ZMCM02()
        {
            InitializeComponent();
            this.BeforeAction += OnBeforeAction;
        }

        public override void Initialize(EIInfo initData)
        {
            initData.AddBlock(_block.GetMaterialStatus());//在用/作废下拉框
            base.Initialize(initData);
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            if("0".Equals(this.treeIsAddmaterial.Value.ToString()))
            {
                this.OpenEFWindowAlert("该节点下不允许加入物资资料", true);
                return;
            }
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "材料基础信息-新增", "insert", null, sender, e);
            
        }

        /// <summary>
        /// 修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if(_list.Count > 0 )
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup(eiInfo, "材料基础信息-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 点击作废事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnModifystopButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnModifyButtonClick("ZMCM02", "modifyMaterialStop", "作废", sender, e);
        }


        /// <summary>
        /// 点击激活事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnModifystartButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnModifyButtonClick("ZMCM02", "modifyMaterialStart", "激活", sender, e);
        }


        /// <summary>
        /// 明细
        /// By 杨鸣 2011.5.30 10:41
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            eIBlock.SetRow(0, row == null ? new EIRow() : row);
            eiInfo.AddBlock(eIBlock);

            base.OnHyperlinkButtonClickPopup(eiInfo, "材料基础信息-查看", "detail", null, sender, e);
        }


        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo eIInfo, string title, string dialogType, string _args)
        {
            MaterialBasicInfo materialBasicInfo = new MaterialBasicInfo(dialogType);//构造一个新的修改页面
            
            //eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cm.isAddTypeInfo"));
            eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.units"));

            materialBasicInfo.EIDataContext = new Platform.Core.UI.EIDataContext();
            materialBasicInfo.EIDataContext.Update(eIInfo);//数据渲染

            if (dialogType.Equals("insert") || dialogType.Equals("update"))
            {
                ((materialBasicInfo.fdMaterialTypeId.Content) as CmesCustomView).eViewInitParams =
                    "bindingService=ZMCM02.type,columns=fdTypeAllCode|fdTypeName,queryConditions=fdTypeAllCode|fdTypeName,initParam=fdTypeInnerCode:" + this.treeTypeInnerCode.Value;
                materialBasicInfo.fdMaterialCode.Visibility = Visibility.Collapsed;
            }

            if (dialogType.Equals("update") || dialogType.Equals("detail"))
            {
                ((materialBasicInfo.fdMaterialTypeId.Content) as CmesCustomView).Display 
                    = eIInfo.GetBlock("result").GetRow(0)["fdMaterialType"].ToString();
                ((materialBasicInfo.fdMaterialTypeId.Content) as CmesCustomView).Value 
                    = eIInfo.GetBlock("result").GetRow(0)["fdMaterialTypeId"].ToString();
            }

            materialBasicInfo.fdParentLevel.Value = this.treeTypeId.Value;//父级模块级别
            materialBasicInfo.isLeaf.Value = this.treeTypeInnerCode.Value;//是否叶子节点
            materialBasicInfo.isLeaf.Value = this.treeTypeCode.Value;//是否叶子节点

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = title == null ? "" : title;//设置Title
            _popup.Content = materialBasicInfo;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(InsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }
            //按钮生效
            base.TempButtonOpen(true);
            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }
    }
}
